/**
 * wanyuaning 17-10-07
 *
 * @param e 计算相对数据所依赖
 * @param option{开始，过程，结束，复合结果句柄，自动处理}
 * @param option.auto
 *          {类型，目标列表}
 *                  目标List{ dom , style初始}
 *
 * lim.start(e , {
            start: function () { console.log('start') },
            process: function (ox, oy, x, y) { console.log(ox) },
            end: function (ox, oy, time) { console.log('time:', time) },    //正常返回(偏移x,偏移y,用时)
            rich: function (res) { console.log('Rich Result:', res) },    //返回丰富信息{x, y, ox, oy, fx, time}
            auto: [
                { type: 'MOVE', target: [ { dom:tar, style:{left: l, top: t } } ] }
            ]
        })

 * 外部使计时器
 * var timer = Lim.timer();
 timer.start();    //计时开始
 timer.end();    //计时结束，返回时长(毫秒)
 *
 * 外部事件邦定
 * LIM.bind('mousemove', D, function (e) { console.log(9) });
 *
 * 系统信息
 * W.LIM.system    //{isAndroid, isIphone, isWinphone, isIpad, isIpod, isPC}
 *
 * 流程事件
 * dragstart dragmove dragend
 *
 * 状态事件
 * swipe tap doubletap
 *
 * 依赖 element.js
 */

(function(W, D){
    function FUC() { return function () {} }
    W.LIM = {
        CAN_MOVE: false,
        EVENT_PROCESS: { START: FUC(), PROCESS: FUC(), END: FUC() }
    };

    W.LIM.system = (function () {
        var userAgentInfo = navigator.userAgent;
        var agents = {isAndroid:"Android", isIphone:"iPhone", isWinphone:"Windows Phone", isIpad:"iPad", isIpod:"iPod"};
        var pc = true;
        for (var i in agents) { if(userAgentInfo.indexOf(agents[i]) > 0){ agents[i] = true; pc = false }else{ agents[i] = false } }
        agents.isPC = pc;
        return agents;
    }());
    W.LIM.timer = function () {
        var ts, te;
        function fnStart() { ts = new Date().getTime() }
        function fnEnd() { te = new Date().getTime(); return te - ts }
        return { start: fnStart, end: fnEnd }
    };
    W.LIM.bind = (function () {
        var events = {
            'mousedown':'mousedown',
            'mousemove':'mousemove',
            'mouseup':'mouseup',
            'touchstart':'touchstart',
            'touchmove':'touchmove',
            'touchend':'touchend',
            'dragstart':LIM.system.isPC?'mousedown':'touchstart',
            'dragmove':LIM.system.isPC?'mousemove':'touchmove',
            'dragend':LIM.system.isPC?'mouseup':'touchend'
        };
        if (window.addEventListener) {
            return function (type, element, fun) { element.addEventListener(events[type], fun, false) }
        }else if(window.attachEvent){
            return function (type, element, fun) { element.attachEvent('on' + events[type], fun) }
        }else{
            return function (type, element, fun) { element['on' + events[type]] = fun }
        }
    }());

    var X, Y;

    LIM.bind('dragmove', D, function (e) {
        if (LIM.CAN_MOVE) {
            X = e.pageX || e.targetTouches[0].pageX || 0;
            Y = e.pageY || e.targetTouches[0].pageY || 0;
            LIM.EVENT_PROCESS.PROCESS(X, Y);
        }
    });
    LIM.bind('dragend', D, function (e) {
        if (LIM.CAN_MOVE) {
            LIM.CAN_MOVE = false;
            LIM.EVENT_PROCESS.END(X, Y);
            LIM.EVENT_PROCESS.END = FUC();
            LIM.EVENT_PROCESS.PROCESS = FUC();
            X = null; Y = null;
        }
    });


    function autoPlay(autoList, ox, oy){
        for(var i=0; i<autoList.length; i++){
            var list = autoList[i],
                type = list.type,
                targets = list.target;

            if(!type){ console.error('LIM[auto]','请指定内置动作类型!'); return }
            if(!targets || targets.length<1){ console.error('LIM[auto]','内置动作缺少目标Dom节点!'); return }

            if(type=='MOVE'){
                for(var j=0; j< targets.length; j++){
                    var left= targets[j].style.left+ox;
                    var top= targets[j].style.top+oy;
                    targets[j].dom.style.left = left + 'PX';
                    targets[j].dom.style.top = top + 'PX';
                }
            }
            // 类型补充
        }
    }

    W.lim = new function () {
        var swipe = this,
            timer = LIM.timer();
        var pageX, pageY;

        swipe.start = function (e,option) {

            pageX = e.pageX || e.targetTouches[0].pageX;
            pageY = e.pageY || e.targetTouches[0].pageY;
            timer.start();    //记录开始
            LIM.CAN_MOVE = true;

            /****开始****/
            if (option.start)  option.start(e, pageX, pageY);


            /****过程****/
            LIM.EVENT_PROCESS.PROCESS = function (x, y) {
                if (option.process) option.process(x-pageX, y-pageY, x, y);    //Process 偏移量/绝对值
                if( option.auto ){ autoPlay(option.auto, x-pageX, y-pageY) }
            };
            /****结束****/
            LIM.EVENT_PROCESS.END = function (x, y) {
                var ox = x-pageX, oy = y-pageY;
                var time = timer.end();
                if (option.end) { option.end( ox, oy, time) }
                if (option.rich) {
                    option.rich({x:x, y:y, ox:ox, oy:oy, fx:[ox/Math.abs(ox),oy/Math.abs(oy)], time});
                }
            };
        };
    };
}(window,document));